package com.cheer.enums;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.cheer.vo.SelectOptVO;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.*;
import java.util.stream.Collectors;

@AllArgsConstructor
public enum LectureTypeEnum {

    YOUTH("1", "团省委青年讲师团"),
    BASE("6", "基层讲师团"),
    CITY("2", "市地宣讲团"),
    CITY_COLLEGE("3", "高校宣讲团"),
    CITY_PROVINCE("4", "省直宣讲团"),
    CITY_ENTERPRISE("5", "企业宣讲团");

    @Getter
    private String value;
    @Getter
    private String label;

    /**
     * 获取讲师类型列表
     * @return
     */
    public static List<SelectOptVO> getLectureTypeSelect(){

        List<SelectOptVO> selectOptVOS = new ArrayList<>();
        selectOptVOS.add(new SelectOptVO(YOUTH.value, YOUTH.label, null));

        List<SelectOptVO> cityChild = new ArrayList<>();
        cityChild.add(new SelectOptVO(CITY.value, CITY.label, null));
        cityChild.add(new SelectOptVO(CITY_COLLEGE.value, CITY_COLLEGE.label, null));
        cityChild.add(new SelectOptVO(CITY_PROVINCE.value, CITY_PROVINCE.label, null));
        cityChild.add(new SelectOptVO(CITY_ENTERPRISE.value, CITY_ENTERPRISE.label, null));

        SelectOptVO city = new SelectOptVO(BASE.value, BASE.label, cityChild);
        selectOptVOS.add(city);

        return selectOptVOS;
    }

    public static String getLectureTypes(String types){

        if(StrUtil.isBlank(types)){
            return "";
        }else{
            List<List> belongs = JSONUtil.toList(types, List.class);
            Set<String> belongsSet = new HashSet<>();
            for (int i = 0; i < belongs.size(); i++) {

                List list = belongs.get(i);
                for (int j = 0; j <list.size(); j++) {
                    String typeId = (String)list.get(j);
                    belongsSet.add(typeId);
                }
            }
            List<LectureTypeEnum>  lectureTypeEnums = new ArrayList<>();
            List<String> belongsNodup = belongsSet.stream().sorted().collect(Collectors.toList());
            for (int i = 0; i < belongsNodup.size(); i++) {

                String typeCode = belongsNodup.get(i);
                LectureTypeEnum byTypeCode = getByTypeCode(typeCode);
                if(byTypeCode != null){
                    lectureTypeEnums.add(byTypeCode);
                }
            }
            if(CollectionUtil.isNotEmpty(lectureTypeEnums)){
                return  lectureTypeEnums.stream().map(LectureTypeEnum::getLabel).collect(Collectors.joining(","));
            }
        }

        return "";
    }

    public static LectureTypeEnum getByTypeCode(String typeCode){

        LectureTypeEnum[] values = LectureTypeEnum.values();
        Optional<LectureTypeEnum> first = Arrays.stream(values)
                .filter(resourceEnum -> typeCode.equals(resourceEnum.getValue()))
                .findFirst();

        return first.orElse(null);
    }




}
